﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gendarme.Framework;
using System.Diagnostics;

namespace Commander.Core.AI {
  public class CommanderGA {

    public static Encoding Launch(double mutation, double crossover, int poluation,
                            List<Defect> defects, double focalPoint, string path) {

      var enc = new Encoding(defects);
      enc.FilePath = path;
      var reproducer = new GeneticReproducer(mutation, crossover, poluation, enc);
      Encoding bestOfAll = null;
      double bestScore = -1;
      Stopwatch watch = Stopwatch.StartNew();
			int count = 0;
      do {

        reproducer.PerformCrossover();
        reproducer.PerformMutation();
        var fittest = reproducer.Population.Fittest;

        if (bestOfAll == null || fittest.Fitness > bestOfAll.Fitness) {
          bestOfAll = fittest;
        }

        reproducer.MoveNext();
				count++;
      } while (bestScore < focalPoint && watch.ElapsedMilliseconds < 30000);
			Console.WriteLine("Ran {0} generations to produce score {1}", count, bestOfAll.Fitness);
      return bestOfAll;
    }


    public static Encoding Launch(double p, double p_2, int p_3, System.Collections.ObjectModel.Collection<Defect> defects, double p_4, string p_5) {
      return Launch(p, p_2, p_3, defects.ToList(), p_4, p_5);
    }
  }
}
